package com.peking.donations.web.controller;

import com.peking.donations.base.security.LoggedUser;
import com.peking.donations.base.util.DigitalUtil;
import com.peking.donations.base.util.SecurityUtil;
import com.peking.donations.base.vo.AjaxResponse;
import com.peking.donations.model.CallOrder;
import com.peking.donations.model.DonationUser;
import com.peking.donations.plugins.pay.wx.utils.GlobalConfigure;
import com.peking.donations.service.BizService;
import com.peking.donations.service.UserService;
import com.peking.donations.vo.CallVo;
import com.peking.donations.vo.GiftCallMember;
import com.peking.donations.vo.GiftCallVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * GiftCallController
 * <p>
 * <p>
 * </p>
 *
 * @author Vigor Yuan
 */
@Controller
@RequestMapping("/peking/gift-call")
public class GiftCallController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    BizService bizService;

    @Autowired
    UserService userService;

    @Value("${global.encryptkey}")
    private String encryptkey;

    @Autowired
    private GlobalConfigure globalConfigure;

    /**
     * 红包召集令-第四页
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String giftCall() {
        return "wx/gift-call";
    }

    /**
     * 保存红包召集令
     * 
     * @return
     * 
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public AjaxResponse giftCallSave(@RequestBody CallVo callVo) {
        CallOrder callOrder = new CallOrder();
        try {
            LoggedUser currentUser = SecurityUtil.getCurrentUser();
            callOrder.setUserId(currentUser.getUserId());
            callOrder.setRealName(currentUser.getFullname());
            callOrder.setName(callVo.getName());
            callOrder.setTotalShare(callVo.getTotalShare());
            callOrder = bizService.saveCallOrder(callOrder);

            DonationUser user = userService.findByOpenId(currentUser.getOpenId());
            user.setType(callVo.getuType());
            userService.updateUser(user);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return AjaxResponse.ERROR("GC_SAVE_ERROR");
        }
        return AjaxResponse.OK(callOrder);
    }

    /**
     * 红包召集令发起成功页面-第五页
     * 
     * @param gcId
     * @return
     */
    @RequestMapping(value = "/{gcId}/success", method = RequestMethod.GET)
    public String giftCallSuccess(ModelMap modelMap, @PathVariable(value = "gcId") Integer gcId) {
        Map<String, Integer> sortMap = bizService.findGiftCallSorts(gcId);
        List<GiftCallVo> top10 = bizService.findGiftCallTop10();
        GiftCallVo order = bizService.getGiftCall(gcId);
        modelMap.addAttribute("sortMap", sortMap);
        modelMap.addAttribute("top10", top10);
        // modelMap.addAttribute("top10", JSONObject.toJSONString(top10));
        modelMap.addAttribute("call", order);
        modelMap.addAttribute("callId", gcId);
        modelMap.addAttribute("user", userService.findByOpenId(SecurityUtil.getCurrentUser().getOpenId()));
        modelMap.addAttribute("globalHostName", globalConfigure.getHostname());
        return "wx/gift-call-success";
    }

    /**
     * 红包召集令发起成功页面-第六页
     *
     * @param gcId
     * @return
     */
    @RequestMapping(value = "/{gcId}/gift", method = RequestMethod.GET)
    public String giftCall(ModelMap modelMap, @PathVariable(value = "gcId") int gcId) {
        // String callId = EncryptUtil.decrypt(gcId,encryptkey);
        Map<String, Integer> sortMap = bizService.findGiftCallSorts(gcId);
        List<GiftCallVo> top10 = bizService.findGiftCallTop10();
        GiftCallVo order = bizService.getGiftCall(gcId);
        CallOrder callOrder = bizService.findCallOrderById(gcId);
        modelMap.addAttribute("sortMap", sortMap);
        modelMap.addAttribute("top10", top10);
        modelMap.addAttribute("gcId", gcId);
        modelMap.addAttribute("call", order);
        modelMap.addAttribute("callOrder", callOrder);
        DonationUser donationUser = userService.findById(callOrder.getUserId());

        int progress = order.getGiftBid() <= 0 ? 0
                : new Double(DigitalUtil.div(order.getGiftCount(), order.getGiftBid()) * 100).intValue();

        modelMap.addAttribute("progress", progress);
        DonationUser currentUser = userService.findByOpenId(SecurityUtil.getCurrentUser().getOpenId());
        modelMap.addAttribute("user", currentUser);
        modelMap.addAttribute("callUser", donationUser);
        logger.debug("currentUser" + currentUser.toString());
        return "wx/gift";
    }

    /**
     * 查询top10-第七页
     * 
     * @return
     */
    @RequestMapping(value = "/{gcId}/top10", method = RequestMethod.GET)
    public String giftCallTop10(ModelMap modelMap, @PathVariable(value = "gcId") Integer gcId) {
        Map<String, Integer> sortMap = bizService.findGiftCallSorts(gcId);
        List<GiftCallVo> top10 = bizService.findGiftCallTop10();
        modelMap.addAttribute("sortMap", sortMap);
        modelMap.addAttribute("top10", top10);
        modelMap.addAttribute("call", bizService.getGiftCall(gcId));
        modelMap.addAttribute("callId", gcId);
        return "order";
    }

    /**
     * 查询top10-第七页
     *
     * @return
     */
    @RequestMapping(value = "/top10", method = RequestMethod.GET)
    public String giftCallOrderTop10(ModelMap modelMap, @PathVariable(value = "gcId") Integer gcId) {
        List<GiftCallVo> top10 = bizService.findGiftCallTop10();
        return "order";
    }

    /**
     * 获取该召集令的更多参与人
     * 
     * @param gcId
     * @param page
     * @return
     */
    @RequestMapping(value = "/{gcId}/members", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResponse giftCallMembers(@PathVariable(value = "gcId") int gcId, @RequestParam(name = "page") int page) {
        List<GiftCallMember> members = bizService.findGiftCallMembersTop10(gcId, page);
        return AjaxResponse.OK(members);
    }

}
